package com.yanqu.road.server.cmd.hunt;

import com.yanqu.road.entity.hunt.OpenHuntBoxResult;
import com.yanqu.road.pb.hunt.HuntProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.hunt.HuntMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

@Cmd(code = Protocol.S_HUNT_OPEN_BOX, desc = "打开狩猎宝箱")
public class OpenHuntBoxCmd extends AbstractCommand {

	private String huntBoxUid;

	private OpenHuntBoxResult result;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HuntProto.OpenHuntBoxReqMsg reqMsg = HuntProto.OpenHuntBoxReqMsg.parseFrom(packet.getBytes());
		huntBoxUid = reqMsg.getHuntBoxUid();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		result = HuntMgr.openHuntBox(player, huntBoxUid);
	}

	@Override
	public void sendData(GamePlayer player) {
		if(null != result) {
			HuntProto.OpenHuntBoxRespMsg.Builder respMsg = HuntProto.OpenHuntBoxRespMsg.newBuilder();
			respMsg.setRet(result.getRet());
			if (0 == result.getRet()) {
				respMsg.setReward(result.getReward());
			}
			player.sendPacket(Protocol.U_HUNT_OPEN_BOX, respMsg);
		}
	}

}
